﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.Common;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Filters;
using STF.Blog.WebApp.Areas.ForumManage.Models.WebMenu;
using X.PagedList;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class WebMenuController : Controller
    {
        private IWebMenuService _iwbMenuScv;
        public WebMenuController(IWebMenuService iwbMenuScv)
        {
            _iwbMenuScv = iwbMenuScv;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search, int page = 1)
        { 
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _iwbMenuScv.GetByNameAsync(search);

            var dataList = new List<WebMenuListViewModel>();

            foreach (var item in data)
            {
                var model = new WebMenuListViewModel
                {
                    Id = item.Id,
                    Title = item.Title,
                    Link = item.Link,
                    Icon = item.Icon,
                    CreateTime = item.CreateTime,
                    UpdateTime = item.UpdateTime,
                    ParentTitle = await GetWebMenuTitle(item.ParentId)
                };
                dataList.Add(model);
            }
            ViewBag.Search = search;
            var pages = await dataList.ToPagedListAsync(page,PageConfig.PageSize);

            return View(pages);
        }

        [HttpGet]
        public async Task<IActionResult> Add() 
        {
            return View(new WebMenuAddViewModel());
        }

        [HttpPost]
        public async Task<IActionResult> Add(WebMenuAddViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                int parentId = 0;
                if (model.ParentList[0] == 2)
                {
                    parentId = (int)model.ParentList[1];
                }
                else if (model.ParentList[0] == 3) 
                {
                    parentId = (int)model.ParentList[2];
                }
                var res = await _iwbMenuScv.AddUsersAsync(model.Title,model.Link,model.Icon,parentId);
                if (res > 0)
                    return Redirect("~/ForumManage/WebMenu/Index");
            }
            return View(model);
        }

        public async Task<IActionResult> Delete(int id) 
        {
            await _iwbMenuScv.PutTrashAsync(id);
            return Redirect("~/ForumManage/WebMenu/Index");
        }

        private async Task<string> GetWebMenuTitle(int parentId) 
        {
            if (parentId == 0)
                return "顶级菜单";
            var data = await _iwbMenuScv.GetByIdAsync(parentId);
            return data.Title;
        }

        [HttpGet]
        private async Task<IActionResult> GetSysMenuList(int parentId = 0) 
        {
            var data = await _iwbMenuScv.GetByParentIdAsync(parentId);
            var dataList = data.Select(wm => new WebMenuDropdownViewModel 
            {
                Id = wm.Id,
                Title = wm.Title
            }).ToList();
            return Json(dataList);
        }

    }
}
